package com.mzy.javaapi.Comparator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 另一种比较器：Compartor
 * 如果一个类已经开发完成，但是在此类建立的初期并没有实现Comparable接口，此时肯定是无法进行对象排序操作的，
 * 所以为了解决这一的问题，java又定义了另一个比较器的操作接口 Comparator 此接口定义在java.util包中，接口定义如下：
 *
 * public  interface  Comparator<T>{
 *                  public  int  compare(T o1，T o2);
 *
 *                  boolean  equals(Object  obj);
 *
 * }
 */
class Student {
    private String name;
    private int age;

    public Student(String name,int age ){
        this.name = name;
        this.age = age;
    }

    public String toString(){
        return "姓名："+this.name+", 年龄："+this.age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

 class T {
    public static void main(String[] args) throws Exception{
        Student stu[] = {new Student("张三",23)
                ,new Student("李四",26)
                ,new Student("王五",22)};
        Arrays.sort(stu,new MyComparator());             //对象数组进行排序操作

        List<Student> list = new ArrayList<Student>();
        list.add(new Student("zhangsan",31));
        list.add(new Student("lisi",30));
        list.add(new Student("wangwu",35));
        Collections.sort(list,new MyComparator());      //List集合进行排序操作

        for (int i = 0; i < stu.length; i++) {
            Student s = stu[i];
            System.out.println(s);
        }

        System.out.println("*********");

        for (int i=0;i<list.size();i++){
            Student s = list.get(i);
            System.out.println(s);
        }
    }
}
